package com.mediacloud.app.newsmodule.activity.microlive;

import android.content.Context;
import androidx.annotation.CallSuper;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mediacloud.app.newsmodule.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 */

public abstract class BaseRecyclerAdapterX<T> extends BaseRecyclerAdapter<T> implements View.OnClickListener
{
    @Override
    @CallSuper
    public void onClick(View v)
    {
        Object tag=v.getTag(R.id.recyclerItemTag);
        if(tag==null)
            return;
        int position= tag!=null? (int) tag :0;
        if(itemClickListener!=null)
        {
            itemClickListener.onItemClick(position,true,this);
        }
        onItemClick(position,getItem(position));
    }

    protected void onItemClick(int position,T item)
    {

    }
    protected WeakReference<Context> contextRef;
    public BaseRecyclerAdapterX(Context context)
    {
        super(context);
        this.contextRef=new WeakReference<>(context);
        data=new ArrayList<>();
    }

    public BaseRecyclerAdapterX(List<T> data, Context context)
    {
        super(data,context);
        this.contextRef=new WeakReference<>(context);
        this.data =data;
    }

    public final Context getContext()
    {
        return contextRef.get();
    }

    @CallSuper
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
    {
        holder.itemView.setTag(R.id.recyclerItemTag,position);
        holder.itemView.setTag(R.id.recyclerItemData,getItem(position));
        if(holder!=null&&!holder.itemView.hasOnClickListeners())
        {
            holder.itemView.setOnClickListener(this);
        }
    }
    protected View inflate(@LayoutRes int layoutResId, ViewGroup parent)
    {
        return LayoutInflater.from(getContext()).inflate(layoutResId,parent,false);
    }
}